package com.gopay.bis.service.bank.impl;

import com.gopay.bis.manager.bank.fund.CiticNotifyManager;
import com.gopay.common.FundResult;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.fund.FundCiticService;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.fund.FundGoapayAccChangeNotify;
import com.gopay.common.domain.fund.FundGopayFuploadNotifyEntity;
import com.gopay.common.domain.fund.FundGopayReconFinishNotifyEntity;
import com.gopay.common.domain.fund.vo.PageData;
import com.gopay.common.domain.proccode.ProcCodeInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.proccode.DicProcCodeService;
import com.gopay.common.util.spring.SpringContextUtil;
import com.gopay.remote.bank.FundInteractRemoteService;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 中信基金服务
 * @FundInteractServiceImpl.java
 * @author dong-wang5@hnari.com
 * @2016年6月16日 上午10:55:37  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("fundInteractService")
public class FundInteractServiceImpl implements FundInteractRemoteService {

	private static Logger logger = LoggerFactory.getLogger(FundInteractServiceImpl.class);

	@Resource(name = "citicNotifyManager")
	private CiticNotifyManager citicNotifyManager;

	@Resource(name = "dicProcCodeService")
	private DicProcCodeService dicProcCodeService;

	@Override
	public FundResult send(PageData pageData) {
		logger.error("FundInteractServiceImpl.send银行返回处理pageData对象："+ ToStringBuilder.reflectionToString(pageData));
		FundResult result = new FundResult();
		try {
			// 中信基金处理
			if (StringUtils.equals(BankConstants.BANK_LOGO_CITIC, pageData.getString(BankConstants.BANK_LOGO_CITIC))) {
				// 验签
				FundInteractRemoteService fundService = (FundInteractRemoteService) SpringContextUtil.getBean("CITIC-FUND");
				result = fundService.send(pageData);
				logger.info("中信基金业务处理,【返回结果】："+ToStringBuilder.reflectionToString(result));
				String service = pageData.getString("service");
				if(FundCiticService.FILE_UPLOAD_NOTIFY.equals(service)){
					pageData.remove("service");
					pageData.put("service",FundCiticService.GOPAY_FILE_UPLOAD_NOTIFY);
					service = FundCiticService.GOPAY_FILE_UPLOAD_NOTIFY;
				}
				if ("Y".equals(result.getIsSuccess())){
					//验签成功，落地数据
					if (FundCiticService.GOPAY_FILE_UPLOAD_NOTIFY.equals(service)){
						//根据文件名称先查询数据库中有没有 该文件上传通知，没有再保存，有设置 result.setIsSuccess("F");
						List<FundGopayFuploadNotifyEntity> fileUploadNotifys = citicNotifyManager.queryFileUploadNotify(pageData);
						if(fileUploadNotifys.size()==0){
							//保存文件上传通知数据
							citicNotifyManager.addFileUploadNotify(pageData);
						}else{
							ProcCodeInfo procCodeInfo = dicProcCodeService.getProcCodeInfo(ProcCodeConstants.PROC_CODE_100E6026);
							result.setIsSuccess("F");
							result.setErrorCode(ProcCodeConstants.PROC_CODE_100E6026);
							result.setErrorMsg(procCodeInfo.getMessage());
						}

					}else{
						throw new GopayException(ProcCodeConstants.PROC_CODE_100E8520);//基金支付中暂不支持的接口名称
					}
				}
			}
			logger.error("FundInteractServiceImpl.send验签返回处理pageData对象："+ToStringBuilder.reflectionToString(pageData));
		} catch (GopayException e) {
			logger.error("FundInteractServiceImpl.send bankBack error: ", e);
			ProcCodeInfo procCodeInfo = dicProcCodeService.getProcCodeInfo(e
					.getErrCode());
			result.setErrorCode(e.getErrCode());
			result.setErrorMsg(procCodeInfo.getMessage());
			result.setIsSuccess("F");
		} catch (Exception e) {
			logger.error("FundInteractServiceImpl.send bankBack error: ", e);
			result.setErrorCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setErrorMsg(e.getMessage());
			result.setIsSuccess("F");
		}
		return result;
	}

	@Override
	public FundResult receive(PageData pageData) {
		logger.error("FundInteractServiceImpl.receive银行返回处理pageData对象："+ToStringBuilder.reflectionToString(pageData));
		FundResult result = new FundResult();
		try {
			// 中信基金处理
			if (StringUtils.equals(BankConstants.BANK_LOGO_CITIC, pageData.getString(BankConstants.BANK_LOGO_CITIC))) {
				// 验签
				FundInteractRemoteService fundService = (FundInteractRemoteService) SpringContextUtil.getBean("CITIC-FUND");
				result = fundService.receive(pageData);
				if ("Y".equals(result.getIsSuccess())){
					String service = pageData.getString("service");
					ProcCodeInfo procCodeInfo = dicProcCodeService.getProcCodeInfo(ProcCodeConstants.PROC_CODE_100E6026);
					//验签成功，落地数据
					if (FundCiticService.FILE_UPLOAD_NOTIFY.equals(service)){
						//根据文件名称先查询数据库中有没有 监管行通知国付宝通知，没有再保存，有设置 result.setIsSuccess("F");
						List<FundGopayFuploadNotifyEntity> fileUploadNotifys = citicNotifyManager.queryFileUploadNotify(pageData);
						logger.info("文件上传通知已有记录：" + fileUploadNotifys.size());
						if(fileUploadNotifys.size()==0){
							//保存文件上传通知数据 和 文件
							int i = citicNotifyManager.addFileUploadNotify(pageData);
							logger.info("文件上传通知操作返回参数：" + i+"【文件通知入库异常】");
							if(i == -1){
								result.setIsSuccess("F");
								result.setErrorCode(ProcCodeConstants.PROC_CODE_100F1002);
								result.setErrorMsg(procCodeInfo.getMessage());
							}
						}else{
							result.setIsSuccess("F");
							result.setErrorCode(ProcCodeConstants.PROC_CODE_100E6026);
							result.setErrorMsg(procCodeInfo.getMessage());
						}

					}else if (FundCiticService.BALANCE_CHANGE_NOTIFY.equals(service)){
						//根据流水号先查询数据库中有没有账户变动通知，没有再保存，有设置 result.setIsSuccess("F");
						List<FundGoapayAccChangeNotify> accChangeNotifys = citicNotifyManager.queryAccChangeNotify(pageData);
						if(accChangeNotifys.size()==0){
							//保存账户变动通知数据
							citicNotifyManager.addAccountChangeNotify(pageData);
						}else{
							result.setIsSuccess("F");
							result.setErrorCode(ProcCodeConstants.PROC_CODE_100E6026);
							result.setErrorMsg(procCodeInfo.getMessage());
						}

					}else if (FundCiticService.RECONCILIATION_FINISH_NOTIFY.equals(service)){
						//根据流水号先查询数据库中有没有对账完成通知，没有再保存，有设置 result.setIsSuccess("F");
						List<FundGopayReconFinishNotifyEntity> reconFinishNotifys = citicNotifyManager.queryReconFinishNotify(pageData);
						if(reconFinishNotifys.size()==0){
							//保存对账完成通知  处理差异文件和订单
							citicNotifyManager.addReconFinishNotify(pageData);
						}else{
							result.setIsSuccess("F");
							result.setErrorCode(ProcCodeConstants.PROC_CODE_100E6026);
							result.setErrorMsg(procCodeInfo.getMessage());
						}

					}else{
						throw new GopayException(ProcCodeConstants.PROC_CODE_100E8520);//基金支付中暂不支持的接口名称
					}
				}
			}
			logger.error("FundInteractServiceImpl.receive验签返回处理pageData对象："+ToStringBuilder.reflectionToString(pageData));
		} catch (GopayException e) {
			logger.error("FundInteractServiceImpl.receive bankBack error: ", e);
			ProcCodeInfo procCodeInfo = dicProcCodeService.getProcCodeInfo(e
					.getErrCode());
			result.setErrorCode(e.getErrCode());
			result.setErrorMsg(procCodeInfo.getMessage());
			result.setIsSuccess("F");
		} catch (Exception e) {
			logger.error("FundInteractServiceImpl.receive bankBack error: ", e);
			result.setErrorCode(ProcCodeConstants.PROC_CODE_100F1002);
			result.setErrorMsg(e.getMessage());
			result.setIsSuccess("F");
		}
		return result;
	}
}